<?php
/**
 * 部门管理
 *
 * @author Dracowyn
 * @since 2023-11-17 17:02
 */

// 引入配置文件
include_once('config/config.php');
$loginAuth = check_auth();

$page = $_GET['page'] ?? 1;

$limit = 10;

$countSql = "SELECT COUNT(`id`) AS count FROM `pre_department`";
$count = find($countSql);

$offset = ($page - 1) * $limit;
$depSql =
	"SELECT depa.id, depa.name,
       COUNT(job.id) AS cjob,
       COUNT(person.id) AS cperson
	FROM pre_department depa
         LEFT JOIN pre_job job ON depa.id = job.depid
         LEFT JOIN pre_person person ON depa.id = person.depid
	GROUP BY depa.id
	LIMIT $offset,$limit";
$depData = all($depSql);
$showPage = showPage($page, $count['count'], $limit);
?>

<html lang="zh">
<head>
	<?php include_once('common/meta.php'); ?>
    <title>部门管理</title>
</head>

<?php include_once('common/header.php'); ?>

<?php include_once('common/menu.php'); ?>

<div class="content">
    <div class="header">
        <h1 class="page-title">部门列表</h1>
    </div>
    <ul class="breadcrumb">
        <li><a href="index.php">首页</a><span class="divider">/</span></li>
        <li class="active">部门管理</li>
    </ul>

    <div class="container-fluid">
        <div class="row-fluid">
            <div class="btn-toolbar">
                <button class="btn btn-primary" onclick="location='depAdd.php'"><i class="icon-plus"></i>添加部门
                </button>
            </div>
            <div class="well">
                <table class="table">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>部门</th>
                        <th>职位数量</th>
                        <th>员工数量</th>
                        <th style="width: 26px"></th>
                    </tr>
                    </thead>
                    <tbody>
					<?php foreach ($depData as $item) { ?>
                        <tr>
                            <td><?php echo $item['id']; ?></td>
                            <td><?php echo $item['name']; ?></td>
							<td><?php echo $item['cjob']; ?></td>
							<td><?php echo $item['cperson']; ?></td>
                            <td>
                                <a href="/depEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
                                <a href="#myModal" role="button" data-toggle="modal"
                                   data-id="<?php echo $item['id'] ?>"><i class="icon-remove"></i></a>
                            </td>
                        </tr>
					<?php } ?>
                    </tbody>
                </table>
            </div>
            <div class="pagination">
				<?php echo $showPage; ?>
            </div>

            <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                 aria-hidden="true">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">确认删除</h3>
                </div>
                <div class="modal-body">
                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>你确定删除该部门？</p>
                </div>
                <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                    <button class="btn btn-danger" data-dismiss="modal">删除</button>
                </div>
            </div>

			<?php include_once('common/footer.php'); ?>
        </div>
    </div>
</div>
<?php include_once('common/script.php'); ?>
<script>
    let id = 0;
    $('a[href="#myModal"]').click(function () {
        id = $(this).data('id');
    });

    $('.btn-danger').click(function () {
        $.ajax({
            type: 'post',
            url: 'ajax.php',
            data: {
                action: 'del',
                table: 'department',
                id,
                adminId: '<?php echo $loginAuth['id']; ?>'
            },
            dataType: 'json',
            success: function (res) {
                if (res) {
                    if (res.code === 1) {
                        alert(res.msg)
                        location.reload();
                    } else {
                        alert(res.msg);
                    }
                }
            }
        })
    })
</script>
</html>

